package unclassified.simple;

public class ShortestCompletingWord {

    public String shortestCompletingWord(String licensePlate, String[] words) {
        int[] c = getWords(licensePlate);
        String ans = null;
        for (String s : words) {
            int[] cur = getWords(s);
            boolean ok = true;
            for (int i = 0; i < 26 && ok; i++) {
                if (c[i] > cur[i]) ok = false;
            }
            if (ok && (ans == null || ans.length() > s.length())) ans = s;
        }
        return ans;
    }

    public int[] getWords(String str){
        int[] c = new int[26];
        for (int i = 0; i < str.length(); i++) {
            if (Character.isLetter(str.charAt(i))) {
                c[Character.toLowerCase(str.charAt(i)) - 'a']++;
            }
        }
        return c;
    }

}
